package com.erclab.xpresso
{
	import mx.rpc.http.mxml.HTTPService;
	import flash.utils.*;
	import flash.display.DisplayObject;
	import mx.collections.ArrayCollection;
	import mx.core.UIComponent;
	
	import com.erclab.xpresso.formelements.*;
	import com.erclab.xpresso.forms.Form;
	import com.erclab.xpresso.util.UIUtil;
	
	public class FormManager
	{

		protected var dynamicElements:ArrayCollection;
		public var theForm:Form;
		
		public function FormManager(formName:String){
			theForm = new Form(formName);
		}
		
		protected function initElements():void{
			var dummy1:CheckBox = null;
			var dummy2:TextInput = null;
			var dummy3:Label = null;
			var dummy4:Image = null;
			var dummy5:Hidden = null;
			var dummy6:DropDown = null;
			var dummy7:TextInputFI = null;
			var dummy8:TextArea = null;

		}

		public function createForm(formService:HTTPService, 
									attachingElement:UIComponent):void{

			theForm.title = formService.lastResult.form.title;
			theForm.uniqueKeyName = formService.lastResult.form.uniqueKeyName;
			theForm.parentKeyName = formService.lastResult.form.parentKeyName;
			
			var elements:ArrayCollection = formService.lastResult.form.formElement as ArrayCollection;
			dynamicElements = new ArrayCollection();

			UIUtil.clearAttachingElement(attachingElement);
			for each ( var element:Object in elements) {
				
				initElements();
				
				var ClassReference:Class = getDefinitionByName(element.dataType) as Class;
	            var instance:DisplayObject = new ClassReference();
				instance.name = element.id;

	            dynamicElements.addItem(instance);
	            if (attachingElement != null) {
		            attachingElement.addChild(UIComponent(instance));
		            var fe:FormElement = FormElement(instance);
		            fe.createLabel(attachingElement, element.label);
		            fe.setAttribute("destinationColumn", element.DestinationColumn);
		            fe.setAttribute("styleName", element.Style);

		            var parameters:Object = element.parameters as Object;
					for (var p:String in element.parameters){
						fe.setAttribute(p, element.parameters[p]);
					}
	            }
			}
						
		}
		
		public function sendForm(formService:HTTPService,
									action:String):void{
				var params:Object = new Object();
				params.action = action;
				params.formName = theForm.formName;
				for each (var element:FormElement in dynamicElements){
					
					var theName:String = element.getAttribute("destinationColumn");
					var unique:String  = theForm.uniqueKeyName;
					
					params[element.getName()] = element.getValue();
					if (element.getAttribute("destinationColumn").toUpperCase() == theForm.uniqueKeyName.toUpperCase()){
						params.uniqueKeyValue = element.getValue();
						if (action == "insert"){
							params[element.getName()] = "";
						}
					}
				}
				if (action == "insert"){
					params.uniqueKeyValue = theForm.parentKeyValue;
				}
				formService.send(params);

		}
		
		public function getFormElements():ArrayCollection{
			return this.dynamicElements;
		}
		
		public function clearForm():void{
			for each (var element:FormElement in dynamicElements){
				element.prepareForInsert();				
			}			
		}
		
		
	}
}